package com.bicap.cloud.nettyserver.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Created by Administrator on 2018/8/15 0015.
 */
@Component
//public class DataMessage implements RabbitTemplate.ConfirmCallback{
public class DataMessage {

//    private RabbitTemplate rabbitTemplate;

    public static final String ROUTINGKEY = "monitor_task_queue";

    public static final String SEARCH_ROUTINGKEY = "monitor_finally_data_queue";

    private Logger logger = LoggerFactory.getLogger(DataMessage.class);

    @Autowired
    private NettyServer nettyServer;

//    @Override
//    public void confirm(CorrelationData correlationData, boolean ack, String s) {
//        if(ack){
//            System.out.println("message send success");
//        }else{
//            System.out.println("message send failed");
//        }
//    }

    /**
     * 构造方法注入
     */
//    @Autowired
//    public DataMessage(RabbitTemplate rabbitTemplate) {
//        this.rabbitTemplate = rabbitTemplate;
//        rabbitTemplate.setConfirmCallback(this);
//    }

//    public void sendOrder(String content) {
//        rabbitTemplate.convertAndSend(ROUTINGKEY,content);
//        System.out.println("任务发送成功");
//    }


//    @RabbitListener(queues = SEARCH_ROUTINGKEY,containerFactory = "pointTaskContainerFactory")
//    public void process(Message message) {
//        String json = new String(message.getBody());
//        try {
//
//            Map map = JSON.parseObject(json);
//            String ip = (String) map.get("ipaddr");
//            if (!StringUtils.isEmpty(ip)) {
//                System.out.println("ip::" + ip);
//                Channel channel = nettyServer.getServerDataHandler().getChanelMap().get(ip);
//                map.remove("ipaddr");
//                String result = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
//                channel.writeAndFlush(result + "\r\n");
//            }
//        }catch(Exception e){
//            logger.info("出错的数据",json);
//        }
//    }

//    @Bean("pointTaskContainerFactory")
//    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setTaskExecutor(Executors.newFixedThreadPool(200));
//        factory.setConcurrentConsumers(100);
//        factory.setPrefetchCount(10);
//        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
}
